package com.exchange.websocket.controller;

import com.exchange.websocket.common.bittrex.BittrexExchange;
import com.exchange.websocket.common.exchangeutils.*;
import com.exchange.websocket.common.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @author zhangjin
 * @since 16/10/14
 */

@RestController
@RequestMapping(value = "/exchange")
public class ExchangeController {

    @Autowired
    private RedisUtil redisUtil;

    /**
     * type 启动类型
     * @param request
     * @param type
     * @throws Exception
     */
    @RequestMapping(value = "/huobi", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public void huobi(HttpServletRequest request, @ModelAttribute("type") Integer type) throws Exception {
        if(type.equals(1)){
            String urlHuoBi = "wss://api.huobi.pro:443/ws";
            HuobiWebsocketClient huobiWebsocketClient = new HuobiWebsocketClient();
            huobiWebsocketClient.startClient(urlHuoBi,redisUtil);
        }else {
            System.out.println(redisUtil.get("huobiBidPrice"));
            System.out.println(redisUtil.get("huobiBidAmount"));
            System.out.println(redisUtil.get("huobiAskPrice"));
            System.out.println(redisUtil.get("huobiAskAmount"));
//            redisUtil.remove("huobiBidPrice");
//            redisUtil.remove("huobiBidAmount");
//            redisUtil.remove("huobiAskPrice");
//            redisUtil.remove("huobiAskAmount");
        }

    }

//    @RequestMapping(value = "/hitbtc", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
//    public void hitbtc(HttpServletRequest request, @ModelAttribute("accountId") String accountId) throws Exception {
//        String urlHitbtc = "wss://api.hitbtc.com:443/api/2/ws";
//        HitbtcWebsocketClient hitbtcWebsocketClient = new HitbtcWebsocketClient();
//        hitbtcWebsocketClient.startClient(urlHitbtc);
//    }
//
//    @RequestMapping(value = "/fcoin", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
//    public void quoinex(HttpServletRequest request, @ModelAttribute("accountId") String accountId) throws Exception {
//        String urlFcoin = "wss://ws.fcoin.com:443/api/v2/ws";
//        FcoinWebsocketClient fcoinWebsocketClient = new FcoinWebsocketClient();
//        fcoinWebsocketClient.startClient(urlFcoin);
//    }

    @RequestMapping(value = "/bitstamp", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public void bitstamp(HttpServletRequest request, @ModelAttribute("accountId") String accountId) throws Exception {
        BitstampWebSocketClient bitstampWebSocketClient = new BitstampWebSocketClient();
    }

    @RequestMapping(value = "/quoinex", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public void hasAuth(HttpServletRequest request, @ModelAttribute("accountId") String accountId) throws Exception {
        QuoinexWebSocketClient quoinexWebSocketClient = new QuoinexWebSocketClient();
    }

    @RequestMapping(value = "/bittrex", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public void bittrex(HttpServletRequest request, @ModelAttribute("accountId") String accountId) throws Exception {
        BittrexExchange bittrexExchange = new BittrexExchange("284a968bd7df4ff0a4b48c6da7279c67","52d62b67303b4c5295285f7151bf30d3");
            bittrexExchange.onUpdateSummaryState(exchangeSummaryState -> {
                if (exchangeSummaryState.getDeltas().length > 0) {
                    Arrays.stream(exchangeSummaryState.getDeltas())
                            .filter(marketSummary -> marketSummary.getMarketName().equals("BTC-BCC") || marketSummary.getMarketName().equals("BTC-ETH"))
                            .forEach(marketSummary -> System.out.println(
                                    String.format(marketSummary.toString())));
                }
            });
    }
}
